<template>
  <div>
    <TopFilter
      ref="conditionFilter"
      :list="topFilterList"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="potenCus_customer_quotedprice_index_01"
    >
      <template slot="controls" slot-scope="props">
        <!-- <el-button size="small" type="primary" @click="addHandle">新增</el-button> -->
        <multiuse-button size="small" type="primary" :click="addHandle" :auth-list="auths" auth-mark="/api/cyxdms.clue/customerPrice/insertCustomerPrice">新增</multiuse-button>
        <multiuse-button size="small" type="primary" :loading="exportLoading" :click="exportHandle" :auth-list="auths" auth-mark="/api/cyxdms.clue/customerPrice/exportCustomerPrice">导出</multiuse-button>
        <!-- <el-button size="small" type="primary" :loading="exportLoading" @click="exportHandle">导出</el-button> -->
      </template>
    </FilterTable>
    <Drawer
      :visible.sync="visible"
      :title="dialogTitle"
      :width="1080"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <quotedpriceModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>
  </div>
</template>
<script>
import { queryQuotationList, queryQuotationListNew, queryEmployees,getQuotationExcelList } from '@/api/potenCus/customer/quotedprice';
import { dictionary } from '@/utils/dictMixin';
import quotedpriceModal from './quotedpriceModal';
import { authority } from '@/utils/authMixin';
import { exportExcelFile} from '@/utils/index';
export default {
  name: 'quotedprice',
  mixins: [dictionary, authority],
  components: {
    quotedpriceModal
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      // fetchApi: queryQuotationList,
      fetchApi: queryQuotationListNew,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { add: '新增', edit: '编辑', show: '明细' },
      exportLoading:false
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'potentialCustomerNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'mobilePhone',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '销售顾问',
          fieldName: 'consultant',
          placeholder: '请选择...',
          itemList: this.createDictList('1045')
        },
        {
          type: 'RANGE_DATE',
          label: '报价时间',
          fieldName: 'time',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '购置方式',
          fieldName: 'purchaseMode',
          placeholder: '请选择...',
          itemList: this.createDictList('1426')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button
                  size="mini"
                  type="text"
                  divider="after"
                  onClick={() => this.editHandle(props.row)}
                  auth-list={this.auths}
                  auth-mark={'/api/cyxdms.clue/customerPrice/quotationModification'}
                >
                  编辑
                </multiuse-button>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '报价时间',
          width: 150,
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售顾问',
          dataIndex: 'consultantName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户编号',
          width: 140,
          dataIndex: 'potentialCustomerNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '手机号码',
          dataIndex: 'mobilePhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '购买车型',
          width: 200,
          dataIndex: 'intentCar',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '指导价（元）',
          dataIndex: 'salesGuidancePrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '优惠金额（元）',
          dataIndex: 'discountPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '裸车价（元）',
          dataIndex: 'nakedCarPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '购置方式',
          dataIndex: 'purchaseMode',
          width: 120,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1426'),
          render: props => {
            return <span>{this.createDictText(props.row.purchaseMode, '1426')}</span>;
          }
        },
        {
          title: '合计（元）',
          dataIndex: 'totalPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '购车税（元）',
          dataIndex: 'purchaseTax',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '保险费（元）',
          dataIndex: 'insurancePremiumPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上牌费（元）',
          dataIndex: 'licensingFeePrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '精品装潢（元）',
          dataIndex: 'fineDecorationPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '其他（元）',
          dataIndex: 'otherPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金融产品费（元）',
          dataIndex: 'financeProductPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '首付金额（元）',
          dataIndex: 'downPayments',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '贷款额（元）',
          dataIndex: 'loanAmountPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分期数',
          dataIndex: 'monthlyNumber',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '贷款年利率',
          dataIndex: 'loanRate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '每期还款金额（元）',
          dataIndex: 'monthlySupply',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.formData = {};
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      console.log(row);
      this.showHandler();
    },
    showHandler() {
      this.dialogTitle = `报价管理${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      if (val.time) {
        val.startTime = val.time[0];
        val.endTime = val.time[1];
      } else {
        val.startTime = '';
        val.endTime = '';
      }
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async getEmployeeList() {
      const res = await queryEmployees();
      // console.log('销售顾问', res)
      if (res.resultCode === 200) {
        this.topFilterList.find(item => item.fieldName === 'consultant').itemList = res.data.map(x => ({ value: x.employeeNo, text: x.employeeName }));
        // this.topFilterList = [...this.topFilterList]
      }
    },
    //报价管理导出
    async exportHandle(){
      let r = await this.$refs.conditionFilter.GET_FORM_DATA();
      let p = null;
      if (r != null && r.length > 1) {
        p = r[1];
      }
      if(p.time){
        p.startTime = p.time[0];
        p.endTime = p.time[1];
      } else {
        p.startTime = '';
        p.endTime = '';
      }
      this.exportLoading = true;
      getQuotationExcelList(p).then(res => {
        exportExcelFile(res.data, '报价管理');
        this.exportLoading = false;
      });
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getEmployeeList();
  }
};
</script>

<style lang="less" scoped>
</style>
